package visao.curso;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

import modelagem.Curso;
import modelagem.Professor;

import com.towel.el.annotation.AnnotationResolver;
import com.towel.swing.table.ObjectTableModel;

import controle.Curso.AcoesInterfaceCurso;
import controle.Curso.cursoDAO;

@SuppressWarnings("serial")
public class interfaceCurso extends JDialog {
	private JButton btnOk, btnCancel, btnRemover;
	private BorderLayout bord = new BorderLayout();
	private JPanel PainelCadastros = new JPanel(), PainelFinal = new JPanel();
	private AcoesInterfaceCurso AcoesInterfaceCurso;
	private ObjectTableModel<Curso> modeloTabela;
	private AnnotationResolver aR;
	private JTextField textNome;
	private JTextField textCodigo;
	private JTextField textCargaHoraria;
	private JLabel lblValor;
	private JTextField textValor;
	private JTable table;
	
	public interfaceCurso(String nada){
		
	}
	
	public interfaceCurso() {
		setModal(true);
		AcoesInterfaceCurso = new AcoesInterfaceCurso(this);
		SetupTela();
		setupTelaFinal();
		Init();
	}
	
	private void SetupTela() {
		this.setTitle("Cadastro de Curso:");
		getContentPane().setLayout(bord);
		bord.setHgap(3);
		bord.setVgap(10);
		this.setSize(505, 400);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setModal(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	@SuppressWarnings("static-access")
	private void Init() {
		getContentPane().add(PainelCadastros, bord.CENTER);
		PainelCadastros.setLayout(null);
		
		JLabel lblNomeDoCurso = new JLabel("Nome do curso:");
		lblNomeDoCurso.setBounds(10, 11, 118, 14);
		PainelCadastros.add(lblNomeDoCurso);
		
		textNome = new JTextField();
		textNome.setBounds(106, 8, 227, 20);
		PainelCadastros.add(textNome);
		textNome.setColumns(10);
		
		JLabel lblCodigo = new JLabel("C\u00F3digo:");
		lblCodigo.setBounds(354, 11, 46, 14);
		PainelCadastros.add(lblCodigo);
		
		textCodigo = new JTextField();
		textCodigo.setBounds(403, 8, 86, 20);
		PainelCadastros.add(textCodigo);
		textCodigo.setColumns(10);
		
		JLabel lblCargaHoraria = new JLabel("Carga Horaria:");
		lblCargaHoraria.setBounds(10, 36, 86, 14);
		PainelCadastros.add(lblCargaHoraria);
		
		textCargaHoraria = new JTextField();
		textCargaHoraria.setBounds(103, 33, 99, 20);
		PainelCadastros.add(textCargaHoraria);
		textCargaHoraria.setColumns(10);
		
		lblValor = new JLabel("Valor:");
		lblValor.setBounds(237, 36, 46, 14);
		PainelCadastros.add(lblValor);
		
		textValor = new JTextField();
		textValor.setBounds(280, 33, 86, 20);
		PainelCadastros.add(textValor);
		textValor.setColumns(10);
		
		
		
		aR = new AnnotationResolver(Curso.class);
		modeloTabela = new ObjectTableModel<Curso>(aR, "codigo,nome,cargaHoraria,valor");
		cursoDAO bd = new cursoDAO();
		modeloTabela.addAll(bd.buscar(""));
		
		table = new JTable(modeloTabela);
		
		table.getColumnModel().getColumn(0).setPreferredWidth(20);
		table.getColumnModel().getColumn(1).setPreferredWidth(200);
		table.getColumnModel().getColumn(2).setPreferredWidth(50);
		table.getColumnModel().getColumn(3).setPreferredWidth(40);
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 56, 479, 241);
		PainelCadastros.add(scrollPane);
		getContentPane().add(PainelFinal, bord.SOUTH);
		
	}
	
	private MaskFormatter setMascara(String mascara){  
	      MaskFormatter mask = null;  
	      try{  
	         mask = new MaskFormatter(mascara);                    
	         }catch(java.text.ParseException ex){}  
	      return mask;  
	   } 

	
	private void setupTelaFinal() {
		
		PainelFinal.setLayout(new GridLayout(2, 1));
		//Botoes do PainelFinal
		btnOk = new JButton("Salvar");
		btnOk.setToolTipText("Salvar Curso");
		btnOk.setName("OK");
		btnOk.setSize(20, 50);
		btnOk.addActionListener(AcoesInterfaceCurso);
		btnCancel = new JButton("Cancelar");
		btnCancel.setToolTipText("Cancelar");
		btnCancel.setName("Cancelar");
		btnCancel.setSize(20, 50);
		btnCancel.addActionListener(AcoesInterfaceCurso);
		
		JPanel p = new JPanel();
		p.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 3));
		p.add(btnOk);
		
		btnRemover = new JButton("Remover");
		btnRemover.addActionListener(AcoesInterfaceCurso);
		p.add(btnRemover);
		p.add(btnCancel);
		PainelFinal.add(p);
		
		
	}
	

	public JButton getbtnOk() {
		return btnOk;
	}

	public JButton getBtnRemover() {
		return btnRemover;
	}

	public JButton getbtnCancel() {
		return btnCancel;
	}

	public BorderLayout getBord() {
		return bord;
	}

	public JPanel getPainelCadastros() {
		return PainelCadastros;
	}

	public JPanel getPainelFinal() {
		return PainelFinal;
	}

	public AcoesInterfaceCurso getAcoesInterfaceCurso() {
		return AcoesInterfaceCurso;
	}

	public void setBtnOk(JButton btnOk) {
		this.btnOk = btnOk;
	}

	public void setBtnCancel(JButton btnCancel) {
		this.btnCancel = btnCancel;
	}

	public JTextField getTextNome() {
		return textNome;
	}

	public void setTextNome(JTextField textNome) {
		this.textNome = textNome;
	}

	public JTextField getTextCodigo() {
		return textCodigo;
	}

	public void setTextCodigo(JTextField textCodigo) {
		this.textCodigo = textCodigo;
	}

	public JTextField getTextCargaHoraria() {
		return textCargaHoraria;
	}

	public void setTextCargaHoraria(JTextField textCargaHoraria) {
		this.textCargaHoraria = textCargaHoraria;
	}

	public JTextField getTextValor() {
		return textValor;
	}

	public void setTextValor(JFormattedTextField textValor) {
		this.textValor = textValor;
	}

	public ObjectTableModel<Curso> getModeloTabela() {
		return modeloTabela;
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}
}
